package com.hyts.dbmeta.base;

import com.hyts.dbmeta.config.DbPropertiesConfig;

import java.math.BigDecimal;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.dbmeta.config
 * author:Libo/Alex
 * create-date:2019-06-17 20:21
 * copyright:libo-hyts-github
 * email:libo2dev@aliyun.com
 * description:此类主要用于: 基础链接配置服务接口
 */
public abstract class AbstractConnectionConfig implements BaseConnectionConfig{


    /**
     * 数据库基础连接池配置字符串
     * @return
     */
    public abstract String baseConnectionConfig();

    /**
     * 数据库基础属性配置属性字符串
     * @return
     */
    public abstract String attributeConnectionConfig();

    /**
     * 数据库认证配置
     * @return
     */
    public abstract String authConnectionConfig();

    /**
     * 注入基础连接字符串服务-顺序必须为：ip-port-databaseName
     * @param ipAddress IP地址
     * @param port 端口
     * @param dataBaseName 数据库名称
     * @return 连接字符串
     */
    String buildBaseConnectionConfig(String ipAddress,Integer port,String dataBaseName){
        return String.format(baseConnectionConfig(), ipAddress,port,dataBaseName);
    }

    /**
     * <b>buildAuthConnectionConfig</b>
     * <p>认证操作连接池传入的信息</p>
     * @param userName 用户名
     * @param password 密码
     * @return
     */
    String buildAuthConnectionConfig(String userName,String password){
        return String.format(authConnectionConfig(),userName,password);
    }

    /**
     * <b>buildAttributeConnectionConfig</b>
     * <p>属性操作连接配置传入信息</p>
     * @param encoding
     * @return
     * @exception
     */
    String buildAttributeConnectionConfig(String encoding){
        return String.format(attributeConnectionConfig(), encoding);
    }

    /**
     * <b>获取连接字符串</b>
     * @param port
     * @param dataBaseName
     * @param encoding
     * @return
     */
    public String getConnectionStr(Integer port,String dataBaseName,String encoding){
        BigDecimal bigDecimal = new BigDecimal(123);
        bigDecimal.divide(new BigDecimal(12));
        return getConnectionStr(DbPropertiesConfig.DEFAULT_IP_ADDRESS,port,dataBaseName,encoding);
    }


    /**
     * @param ipAddress
     * @param port
     * @param dataBaseName
     * @return
     */
    public String getConnectionStr(String ipAddress,Integer port,String dataBaseName){
        return buildBaseConnectionConfig(ipAddress,port,dataBaseName);
    }

    /**
     * <b>获取连接字符串</b>
     * @param ipAddress
     * @param port
     * @param dataBaseName
     * @param encoding
     * @return
     */
    public String getConnectionStr(String ipAddress,Integer port,String dataBaseName,String encoding){
        return String.format("%s%s",buildBaseConnectionConfig(ipAddress,port,dataBaseName),buildAttributeConnectionConfig(encoding));
    }

    /**
     * @param port
     * @param dataBaseName
     * @param userName
     * @param password
     * @return
     */
    public String getConnectionStr(Integer port,String dataBaseName,String userName,String password){
        return String.format("%s%s%s",buildBaseConnectionConfig(DbPropertiesConfig.DEFAULT_IP_ADDRESS,port,dataBaseName),
                buildAuthConnectionConfig(userName,password),buildAttributeConnectionConfig(DbPropertiesConfig.DEFAULT_ENCODING));
    }

    /**
     * @param ipAddress
     * @param port
     * @param dataBaseName
     * @param userName
     * @param password
     * @return
     */
    @Override
    public String getConnectionStr(String ipAddress, Integer port, String dataBaseName, String userName, String password){
        return buildBaseConnectionConfig(ipAddress,port,dataBaseName)+buildAuthConnectionConfig(userName,password)+
                    buildAttributeConnectionConfig(DbPropertiesConfig.DEFAULT_ENCODING);
    }

    /**
     * <b>获取连接字符串</b>
     * <p>获取总体信息字符串</p>
     * @param port 端口
     * @param dataBaseName 数据库名称
     * @param userName 用户名
     * @param password 密码
     * @param encoding 编码
     * @return
     * @exception
     */
    @Override
    public String getConnectionStr(Integer port, String dataBaseName, String userName, String password, String encoding){
        return buildBaseConnectionConfig(DbPropertiesConfig.DEFAULT_IP_ADDRESS,port,dataBaseName)+buildAuthConnectionConfig(userName,password)+buildAttributeConnectionConfig(encoding);
    }

    /**
     * @param ipAddress
     * @param port
     * @param dataBaseName
     * @param userName
     * @param password
     * @param encoding
     * @return
     */
    @Override
    public String getConnectionStr(String ipAddress, Integer port, String dataBaseName, String userName, String password, String encoding){
        return buildBaseConnectionConfig(ipAddress,port,dataBaseName)+buildAuthConnectionConfig(userName,password)+buildAttributeConnectionConfig(encoding);
    }
}
